package com.hwj.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;
import com.hwj.service.FollowService;
import com.hwj.utils.JsonResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@CrossOrigin
@RestController
@Slf4j
@RequestMapping("/follow")
public class FollowController {
    @Autowired
    FollowService followService;

    /**
     * 关注、取消关注
     * @param userId
     * @param request
     * @return
     */
    @PostMapping("")
    public JsonResponse followFriend(Long userId, HttpServletRequest request){
        JWT jwt = JWTUtil.parseToken(request.getHeader("token"));
        Long loginId = Convert.toLong(jwt.getPayload("userId"));
        return JsonResponse.successResponse(0,"新增/删除关注记录成功",followService.followFriend(loginId,userId));
    }

    /**
     * 判断是否关注
     */
    @GetMapping("")
    public JsonResponse flagFollow(Long userId,Long followUserId){
        return JsonResponse.successResponse(0,"查询成功",followService.flagFollow(userId,followUserId));
    }
}
